package com.creating.mict.dao;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.creating.mict.domain.Column;
import com.creating.mict.domain.LisRecord;
import com.creating.mict.domain.Patient;
import com.creating.mict.domain.XyjRecord;



/** 
 * @ClassName: XyjDao 
 * @Description: 血压计模块Dao
 * @author xuelei
 * @date 2012-3-9 下午7:53:55 
 *  
 */
public class LisDao extends HibernateDaoSupport {

private static final Log log = LogFactory.getLog(LisDao.class);
	
	
/**
 * 
* @Title: queryLisResult 
* @Description: 获取LIS检查结果
* @param @param fbtime
* @param @param fetime
* @param @param fkeywords
* @param @return 
* @return List<LisRecord>    返回类型 
* @throws
 */
public List<LisRecord> queryLisResult(String fbtime, String fetime, String fkeywords) {
		
		String sql = "";
		sql = " select a.fpatientid, a.fdesc, a.fdate, a.fvalue, a.freference, a.fdoc, a.fphone, b.fpid, b.fname, b.fybkh "
			+ " from tds_lis_record a, tds_patient b " 
			+ " where a.fpatientid=b.fpid and fsign_state='1' "; 
		
		if(fbtime == null || fbtime.equals("null") || fbtime.equals("")) {
			sql += " and a.fdate <= to_date('" + fetime + " 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
		} else {
			sql += " and a.fdate between to_date('" + fbtime + " 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('" + fetime + " 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
		}
		
		if(fkeywords == null || fkeywords.equals("null")) {
			//do nothing
		} else {
			sql	+= " and (a.fphone like '" + fkeywords + "%' or b.fname like '" + fkeywords + "%') ";
		}
		sql	+= " order by a.fdate desc, a.fname asc, a.forder asc ";
		
		Query query = this.getSession().createSQLQuery(sql);
		List list = query.list();
		List<LisRecord> lr_list = new ArrayList<LisRecord>();
		
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);
			LisRecord lr = new LisRecord();
			for (int j = 0; j < obj.length; j++) {
				lr.setFname("" + obj[8]);
				lr.setFdesc("" + obj[1]);
				lr.setFvalue("" + obj[3]);
				lr.setFphone("" + obj[6]);
				lr.setFreference("" + obj[4]);
				lr.setFdoc("" + obj[5]);
				lr.setFdateStr("" + obj[2]);
			}
			lr_list.add(lr);
		}
		return lr_list;
	}
	

	
	
}
